package com.seeyon.apps.kk.manager.restImpl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.seeyon.apps.kk.manager.BaseManager;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;

/**
 * rest 计量接口实现
 * 
 * @author seeyon
 *
 */
public class MeasuringEquipmentImpl extends BaseManager {
	private static final Log log = LogFactory.getLog(MeasuringEquipmentImpl.class);

	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	/**
	 * 修改记录
	 */
	public void updCheckoutRecord(Map<String, Object> params) {
		try {
			String sql = "";
			// TODO 自己变更状态为 已检验 正式 1217090911455366817 、测试 -4261600870970933171
			// 已送检 正式 1804752199339753525 、 测试 1534311809652181901
			// 已领取 正式 6170007984298935487 、测试 6170007984298935487
			// 是否 ： 是 -3606019692210796345 否 -4291293164189236870
			String type = MapUtils.getString(params, "type");
			if (StringUtils.equals(type, "2")) {
				// params.put("type", "-4261600870970933171");
				params.put("type", "1217090911455366817");
				if (StringUtils.equals(MapUtils.getString(params, "isQualified"), "Y")) {
					// params.put("isQualified", "-3606019692210796345");
					params.put("isQualified", "-3606019692210796345");

				} else {
					// params.put("isQualified", "-4291293164189236870");
					params.put("isQualified", "-4291293164189236870");
				}
				sql = this.freemarkFlowManager.fltTemplateIntoString("datasync/zichankapian/rest/NC_JYJL_UPDATESQL.ftl",
						params);
			} else {
				if (StringUtils.equals(type, "1")) {
					// params.put("type", "1534311809652181901");
					params.put("type", "1804752199339753525");
				}
				if (StringUtils.equals(type, "3")) {
					// params.put("type", "6170007984298935487");
					params.put("type", "6170007984298935487");
				}
				sql = this.freemarkFlowManager.fltTemplateIntoString("datasync/zichankapian/rest/NC_LQJL_UPDATESQL.ftl",
						params);
			}
			log.info("====================修改记录=====================\n" + sql);
			this.jdbcTemplate.update(sql);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			log.info("===================修改记录错误=====================\n" + e.getMessage());

		}
	}

	/**
	 * 合同金额回写
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, Object> writeBackAmount(Map<String, Object> params) {
		Map<String, Object> rsMap = Maps.newHashMap();
		if (params.isEmpty()) {
			rsMap.put("code", "0002");
			rsMap.put("msg", "params is null!");
			return rsMap;
		}
		String sql = "SELECT FIELD0036 money FROM FORMMAIN_2010 WHERE FIELD0004 = '"
				+ MapUtils.getString(params, "contractNo") + "'";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		if (list.isEmpty()) {
			rsMap.put("code", "0002");
			rsMap.put("msg", "合同编号不存在! params:" + params);
			return rsMap;
		}
		log.info("=====shkk 合同付款已付金额：sql："+sql);
		int money = MapUtils.getInteger(list.get(0), "money") + MapUtils.getInteger(params, "money");
		String updateSql = "UPDATE FORMMAIN_2010 SET FIELD0036 = '" + money + "' WHERE FIELD0004 = '"
				+ MapUtils.getString(params, "contractNo") + "'";
		log.info("=====shkk 合同付款金额回写：sql："+updateSql);
		int num = this.jdbcTemplate.update(updateSql);
		rsMap.put("code", "0001");
		rsMap.put("msg", "回写数据（"+num+"）条！");
		return params;
	}
}
